package com.example.presenters;

import java.util.ArrayList;

import com.example.db.db;
import com.example.interfaces.IPostItemActivity;
import com.example.model.Date;
import com.example.model.Item;
import com.example.model.ItemList;
import com.example.model.User;
import com.example.model.UserList;

/**
 * The presenter for posting an item. It will handle creating the item and
 * adding it to the current ItemList.
 * 
 * @author 3Factorial
 */
public class PostItemPresenter {
	private IPostItemActivity post;
	private UserList users;
	private ItemList items;

	/**
	 * Creates a PostItemPresenter with the given UserList, ItemList, and the
	 * post item activity interface.
	 * 
	 * @param post
	 * @param users
	 * @param items
	 */
	public PostItemPresenter(IPostItemActivity post, UserList users,
			ItemList items) {
		this.post = post;
		this.users = users;
		this.items = items;
	}

	/**
	 * Adds the given item to the ItemList.
	 * 
	 * @param name
	 *            The name of the item.
	 * @param des
	 *            The description of the item.
	 * @param loc
	 *            The location of the item.
	 * @param username
	 *            The username of the user that created this item.
	 */
	public void addItem(String name, String des, String loc, String username, Date date, int lost) {
		User u = users.getUserByUsername(username);
		Item item = new Item(name, des, loc, u, date, lost);
		// Gets the tags from the check boxes. If no tags were selected,
		// then the list is not added to the array.
		ArrayList<String> possibleTags = post.getTags();
		if (!possibleTags.isEmpty()) {
			item.setTags(post.getTags());
		}
		u.addItem(item);
		items.add(item);
		db dbaccess = new db();
		dbaccess.addItem(name, loc, des, "", "", username, "date", Integer.toString(lost));
		post.goToProfile();
		// post.goToItemScreen(name);
	}

} // End PostItemPresenter class
